<template>

      <view class="goods-item" @click="itemClickHandler(goods)">
        <!-- 商品左侧图片区域 -->
        <view class="goods-item-left">
          <radio :checked="goods.goods_state" color="#C00000" v-if="showRadio" @click="radioChangeHandler"></radio>
          <image class="goods-image" :src="goods.goods_small_logo || defaultPic"></image>
        </view>
        <!-- 商品右侧信息区域 -->
        <view class="goods-item-right">
          <!-- 商品标题 -->
          <view class="goods-name" @click="test" >{{goods.goods_name}}</view>
          <!-- 商品价格 -->
          <view class="goods-bottom">
            <view class="goods-price">{{goods.goods_price | tofixed | addYuan}}</view>
            <!-- <view class="goods-price">¥{{Number(goods.goods_price).toFixed(2)}}</view> -->
            <!-- <view class="goods-price">¥{{tofixed(goods.goods_price)}}</view> -->
            <!-- <view class="goods-price">¥{{tofixed}}</view> -->
            <uni-number-box :min="1" :value="goods.goods_count" @change="numChangeHandler" v-if="showNumber"></uni-number-box>
            <text @click="deleteGoods">删除</text>
          </view>

        </view>
      </view>

</template>

<script>
  export default {
    name:"my-goods",
    props: {
      // 组件显示的商品
      goods: {
        type: Object,
        default: {}
      },
      // 组件是否显示radio的配置
      showRadio: {
        type: Boolean,
        default: false
      },
      // 组件是否显示numbox
      showNumber: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    filters: { // 格式化处理页面显示，多个函数 连起来调用
      tofixed(num){
        return Number(num).toFixed(2)
      },
      addYuan(param){
        return '¥'+param
      }
    },
    computed: {
      tofixedComputed(){
        return Number(this.goods.goods_price).toFixed(2)
      },
    },
    methods: {
      // 删除商品
      deleteGoods(e){
        this.$emit('delete', {
          // 商品的 Id
          goods_id: this.goods.goods_id,
        })
      },
      
      radioChangeHandler(e){
        this.$emit('radio-change', {
          // 商品的 Id
          goods_id: this.goods.goods_id,
          // 商品最新的勾选状态
          goods_state: !this.goods.goods_state
        })
      },
      
      // 商品数量发生变化监听
      numChangeHandler(value){
        console.log(typeof value, value)
        this.$emit('num-change', {
          goods_id: this.goods.goods_id,
          goods_count: value
        })
      },
      // 点击商品item跳转到商品详情页
      // gotoGoodsDetail(goods){
      //   uni.navigateTo({
      //     url: `/subpkg/goods_tail/goods_tail?goods_id=${goods.goods_id}`
      //   })
      // },
      
      
      itemClickHandler(goods){
        // console.log('====', goods)
        this.$emit('xxx', goods)
        // uni.navigateTo({
        //   url: `/subpkg/goods_detail/goods_detail?goods_id=xxx`
        // })
      },
      
      test(){
        console.log('test')
      },
      
      tofixedMethods(num){
        return Number(num).toFixed(2)
      },
    },
  }
</script>

<style lang="scss">
    .goods-item {
      display: flex;
      border-bottom: 1px solid #efefef;
      padding: 8rpx;
      .goods-item-left {
        display: flex;
        align-items: center;
        .goods-image {
          width: 200rpx;
          height: 200rpx;
          vertical-align: middle;
        }
      }
      .goods-item-right {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .goods-name {
          font-size: 24rpx;
        }
        .goods-bottom {
          display: flex;
          justify-content: space-between;
          .goods-price{
            font-size: 32rpx;
            color: #c00000;
          }
        }

      }
    }
</style>
